package com.gec.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //security规定密码不能明文使用,必须使用加密器加密
        auth.inMemoryAuthentication()
                .withUser("zhangsan")
                .password(passwordEncoder().encode("123"))
                .roles();
        auth.inMemoryAuthentication()
                .withUser("lisi")
                .password(passwordEncoder().encode("1234"))
                .roles();
    }

    //创建密码加密类
    @Bean
    public PasswordEncoder passwordEncoder() {
        //创建实现类,是一种加密算法
        return new BCryptPasswordEncoder();
    }
}
